home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Need HELP by a C hacker....
- Date: 01 Mar 1996 05:42:23 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb29224223@qcd.lanl.gov>
- References: <4h5mhv$mh9@newsstand.cit.cornell.edu>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: NOBODY's message of 1 Mar 1996 02:14:23 GMT
-
- In article <4h5mhv$mh9@newsstand.cit.cornell.edu>
- NOBODY <rezab@nova.npac.syr.edu> writes:
-
- N: I want to declare a function
- <snip>
- N: _____ foo(void){
- N: return foo;
- N: }
- N:
- N: Ususally _____ is the type of the thing that I am returning.
- N: In this case it's a pointer to a function that returns a pointer
- N: to a function. Does anyone have any ideas?
-
- This is a flaw in the type system of C. It does not have the nice
- yin-yang rules, and so, it is not possible to do what you want to do.
-
- There are basically two approaches: return a struct:
-
- struct function { struct function (*fun)(void); }
- foo(void) {
- static struct function retval;
- retval.fun = foo;
- return retval;
- }
-
- And then use foo().fun in the same way that you would normally have
- used foo() in your `version' of the code.
-
- Else, you can use casts: as any pointer to a function can be cast to
- any other (and cast back before calling), you can write
-
- int (*foo(void))(void) {
- return (int(*)(void))foo;
- }
-
- and use ((int(*(*)(void))(void))foo()) instead of foo(). On second
- thoughts, use typedefs to achieve the same purpose :-)
-
- I prefer the first solution even when the second is made even slightly
- legible even to experienced programmers.
-
- N: I have gone nuts trying to figure this one out.
- N: BTW, I am using the gcc compiler (My microsoft compiler does not allow
- N: anything like this to be declared).
-
- As I said, C typesystem is flawed in this regard. I would really have
- liked an incomplete function type:
-
- () functype;
- typedef functype *functype(void);
- where the first line is for ease of parsing, and declares functype to
- be a `function type' to be completed later; and the second is the
- completion. `typedef functype' would have been nice except for
- historical assumption of `int' as the default type; only the second is
- unacceptable because if functype exists as a typedef in an enclosing
- scope, we need ambiguity resolution rules which really make life
- harder for everyone. A `function' keyword would solve the problem
- neatly, but new keywords are never backward compatible.
-
- This, however, is not C.
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-